package com.tengfei.main.course.service.impl;


import com.aliyun.oss.OSS;
import com.tengfei.main.course.config.AliyunConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author feige
 * @version v1.0
 * @date 2022-07-26-15:47
 * @description oss对象存储异步上传
 */
@Slf4j
@Component
public class OssServiceImpl {
    @Resource
    private AliyunConfig aliyunConfig;

    /**
     * 异步上传视频
     * @param videoPath
     * @param f
     */
    @Async
    public void uploadVideo(String videoPath, File f) {
        // 获取实例
        OSS ossClient = aliyunConfig.getInstance();
        // 文件上传
        ossClient.putObject(aliyunConfig.getBucketName(), videoPath, f);
        // 关闭实例
        ossClient.shutdown();
    }

    /**
     * 异步上传文件
     * @param resPath
     * @param file
     */
    @Async
    public void uploadRes(String resPath, InputStream file) {
        OSS ossClient = aliyunConfig.getInstance();
        ossClient.putObject(aliyunConfig.getBucketName(), resPath, file);
        ossClient.shutdown();
        try {
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 同步上传图片
     * @param imgPath
     * @param file
     */
    public void uploadImg(String imgPath, InputStream file) {
        OSS ossClient = aliyunConfig.getInstance();
        ossClient.putObject(aliyunConfig.getBucketName(), imgPath, file);
        ossClient.shutdown();
        try {
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
